<?php

namespace AuctionCore\CarDealer;

use AuctionCore\System\ListBase;

//
// +------------------------------------------------------------------------+
// | PHP Version 5.4                                                        |
// +------------------------------------------------------------------------+
// | Copyright (c) All rights reserved.                                     |
// +------------------------------------------------------------------------+
// | 世纪车来车往(北京）网络科技有限公司 版权所有！                         |
// | TEL:010-63732365  HOTLINE:400-6688-365  EMAIL:clcw@clcw.com.cn         |
// | $Revision: 1.0 $                                                       |
// +------------------------------------------------------------------------+
// | Author: songaimin@clcw.com.cn  Blog:http://blog.csdn.net/samxx8        |
// +------------------------------------------------------------------------+
//

/**
 * "车商保证金"列表
 * @author       爱民
 */
class BailList extends ListBase {

    private $base_sql = null;
    private $count_sql = null;

    public function __construct() {

        /* SELECT d.*,u.uid,u.user_name  FROM au_car_dealer d 
          LEFT JOIN au_dealer_user du ON d.dealer_id=du.dealer_id
          LEFT JOIN au_user u ON du.uid=u.uid
          #condition# #order_by# #limit# */

        $prefix = C('DB_PREFIX');
        $query_sql = "SELECT d.*,u.uid,u.user_name  ";
        $sql = " FROM {$prefix}car_dealer d 
        LEFT JOIN {$prefix}dealer_user du ON d.dealer_id=du.dealer_id 
        LEFT JOIN {$prefix}user u ON du.uid=u.uid  ";
        $this->base_sql = $query_sql . $sql . " #condition# #order_by# #limit# ";
        $this->count_sql = "select count(*) count " . $sql;
    }

    /**
     * 获取列表结果
     * @param    int $ev_page_num    每页条数
     * @param    int $page    页码
     * @return   Object
     */
    public function get($ev_page_num, $page) {
        if (!$this->order_by) {
            $this->order_by = 'ORDER BY dealer_id ASC';
        }


        $dealer_id = $this->dealer_id;
        $Model = new \Think\Model();

        //拍单列表信息
        $sql = $this->base_sql;
        $sql = str_replace('#order_by#', $this->order_by, $sql);
        $sql = str_replace('#condition#', $this->condition, $sql);
        $skip = ($page < 2 ? 0 : $ev_page_num * ($page - 1));
        $limit = " limit {$skip},{$ev_page_num}";
        $sql = str_replace('#limit#', $limit, $sql);
        $sql .= ";";

        //echo $sql;exit;
        $list = $Model->query($sql);
        foreach($list as $k=>$v){
            $list[$k]['bail_amount'] = intval($v['bail_amount']);
            $list[$k]['freeze_amount'] = intval($v['freeze_amount']);
            $list[$k]['indemnity_amount'] = intval($v['indemnity_amount']);
        }

        //dump($list);exit;
        return $list;
    }

    public function count() {
        $Model = new \Think\Model();
        $sql = $this->count_sql;
        $sql .= " {$this->condition}";
        $sql .= ";";
        //echo $sql;exit;
        $r = $Model->query($sql);
        return $r[0]['count'];
    }


    public function get_offline_logs($page=1,$limit=10,$params=array())
    {
        $page = (int)$page < 1 ? 1 : (int)$page;
        $limit = (int)$limit < 1 ? 10 : (int)$limit;
        $map = array();
        $map['recharge_method'] = array('eq',3);
        //车商账号
        if(!empty($params['user_name'])){
            $map['u.user_name'] = array('like',$params['user_name']);
        }
        //联系人
        if(!empty($params['real_name'])){
            $map['u.real_name'] = array('like',$params['real_name']);
        }
        //联系电话
        if(!empty($params['mobile'])){
            $map['u.sms_mobile'] = array('eq',$params['mobile']);
        }
        //充值时间
        if (!empty($params['start_time']) || !empty($params['end_time'])) {
            if (empty($params['start_time'])) {
                $map['dbl.createtime'] = array("elt", $params['end_time']);
            } elseif (empty($params['end_time'])) {
                $map['dbl.createtime'] = array("gt", $params['start_time']);
            } else {
                $map['dbl.createtime'] = array(array("gt", $params['start_time']), array("elt", $params['end_time']));
            }
        }

        $prefix = C('DB_PREFIX');
        $cols = 'du.dealer_id,u.user_name,u.real_name,u.sms_mobile,dbl.occur_money,dbl.bl_id,dbl.createtime';
        $total = M('dealer_bail_log')->alias('dbl')
            ->join($prefix.'dealer_user as du ON dbl.dealer_id=du.dealer_id')
            ->join($prefix.'user as u ON u.uid=du.uid')
            ->where($map)->count();

        $list = M('dealer_bail_log')->alias('dbl')->field($cols)
            ->join($prefix.'dealer_user as du ON dbl.dealer_id=du.dealer_id')
            ->join($prefix.'user as u ON u.uid=du.uid')
            ->where($map)->page($page)->limit($limit)->order('dbl.createtime desc')->select();
        $result = array(
            'list'=> $list,
            'total'=> $total
        );
        return $result;
    }


    public function get_lakala_logs($page=1,$limit=10,$params=array())
    {
        $page = (int)$page < 1 ? 1 : (int)$page;
        $limit = (int)$limit < 1 ? 10 : (int)$limit;
        $map = array();
        $map['recharge_method'] = array('eq',2);
        //车商账号
        if(!empty($params['user_name'])){
            $map['u.user_name'] = array('like',$params['user_name']);
        }
        //联系人
        if(!empty($params['real_name'])){
            $map['u.real_name'] = array('like',$params['real_name']);
        }
        //联系电话
        if(!empty($params['mobile'])){
            $map['u.sms_mobile'] = array('eq',$params['mobile']);
        }
        //充值时间
        if (!empty($params['start_time']) || !empty($params['end_time'])) {
            if (empty($params['start_time'])) {
                $map['lbl.deal_time'] = array("elt", $params['end_time']);
            } elseif (empty($params['end_time'])) {
                $map['lbl.deal_time'] = array("gt", $params['start_time']);
            } else {
                $map['lbl.deal_time'] = array(array("gt", $params['start_time']), array("elt", $params['end_time']));
            }
        }

        $prefix = C('DB_PREFIX');
        $cols = 'du.dealer_id,u.user_name,u.real_name,u.sms_mobile,dbl.occur_money,lbl.order_no,lbl.order_fee,lbl.transaction_id,lbl.deal_time';
        $total = M('dealer_bail_log')->alias('dbl')
            ->join($prefix.'dealer_user as du ON dbl.dealer_id=du.dealer_id')
            ->join($prefix.'user as u ON u.uid=du.uid')
            ->join($prefix.'lakala_bill_log as lbl ON lbl.order_no=dbl.recharge_no')
            ->where($map)->count();

        $list = M('dealer_bail_log')->alias('dbl')->field($cols)
            ->join($prefix.'dealer_user as du ON dbl.dealer_id=du.dealer_id')
            ->join($prefix.'user as u ON u.uid=du.uid')
            ->join($prefix.'lakala_bill_log as lbl ON lbl.order_no=dbl.recharge_no')
            ->where($map)->page($page)->limit($limit)->order('lbl.deal_time desc')->select();

        $result = array(
            'list'=> $list,
            'total'=> $total
        );
        return $result;

    }
}
//end